//
//  LoginViewController.swift
//  微信Swift
//
//  Created by ztDev on 15/10/7.
//  Copyright © 2015年 CC. All rights reserved.
//

import UIKit


// 为UIView扩展属性,可以在storyboard中使用
extension UIView {
    
    // 关键字@IBInspectable, 可以让不可见的属性能够在storyboard中出现,方便设置
    @IBInspectable var cornerRadius: CGFloat {
        
        get {
            
            return layer.cornerRadius
        }
        
        set {
            
            layer.cornerRadius = newValue
            layer.masksToBounds = (newValue > 0)
        }
    }
}

class LoginViewController: UIViewController, JSAnimatedImagesViewDataSource {

    
    @IBOutlet weak var userName: UITextField!
    
    @IBOutlet weak var passWorld: UITextField!
    @IBOutlet weak var loginStackView: UIStackView!
    
    // 轮播图
    @IBOutlet weak var jsAnimatedIV: JSAnimatedImagesView!
    
    
    
    // 即将显示
    override func viewWillAppear(animated: Bool) {
        
        // 隐藏导航栏
        self.navigationController?.navigationBarHidden = true
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // 隐藏导航栏
        self.navigationController?.navigationBarHidden = true
        
        // Do any additional setup after loading the view.
        self.jsAnimatedIV.dataSource = self
        
    }
    
    // 登陆按钮事件
    @IBAction func login(sender: UIButton) {
        
        //查询用户是否已经注册
        let query = AVQuery(className: "XBUser")
        query.whereKey("user", equalTo: self.userName.text)
        query.whereKey("pass", equalTo: self.passWorld.text)
        
        //执行查询
        query.getFirstObjectInBackgroundWithBlock { (object, e) -> Void in
            self.clearAllNotice()
            
            // 用户存在
            if object != nil {
                
                self.successNotice("登陆成功")
                
                // 获取storyborad上的viewcontroller
                let story = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
                
                let mainVC = story.instantiateViewControllerWithIdentifier("UITabBarController-0cI-97-KDB")
                
                self.presentViewController(mainVC, animated: true, completion: nil)
                
            } else {
                
                // 用户未注册
                self.successNotice("请先注册")
                
                
            }
        }
        
    }
    
    // 出现的动画
    override func viewDidAppear(animated: Bool) {
        
        UIView.animateWithDuration(1) { () -> Void in
            
            self.loginStackView.axis = UILayoutConstraintAxis.Vertical
        }
        
    }

    // JSAnimatedImagesViewDataSource
    func animatedImagesNumberOfImages(animatedImagesView: JSAnimatedImagesView!) -> UInt {
        
        return 3
    }
    
    func animatedImagesView(animatedImagesView: JSAnimatedImagesView!, imageAtIndex index: UInt) -> UIImage! {
        
        return UIImage(named: "image\(index + 1)")
        
    }
    

}
